package com.atguigu.gmall.all.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @author tongatong
 * @name IndexController
 * @description
 * @date 2021/9/17
 * @time 20:25
 */
@Controller
public class IndexController {
    @Resource
    private ProductFeignClient productFeignClient;
    @Resource
    private TemplateEngine templateEngine;

    @GetMapping({"/","index.html"})
    public String index(Model model){
        // 获取首页分类数据
        Result<List<JSONObject>> result = productFeignClient.getBaseCategoryList();
        model.addAttribute("list",result.getData());
        return "index/index";
    }

    @GetMapping("createIndex")
    @ResponseBody
    public Result<Void> createIndex(){
        //  获取后台存储的数据
        Result<List<JSONObject>> result = productFeignClient.getBaseCategoryList();
        //  设置模板显示的内容
        Context context = new Context();
        context.setVariable("list",result.getData());

        //  定义文件输入位置
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter("D:\\index.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //  调用process();方法创建模板
        templateEngine.process("index/index.html",context,fileWriter);
        return Result.ok();
    }


}
